import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_note/note/base.dart';
import 'package:flutter_note/note/bean.dart';
import 'package:flutter_note/note/const.dart';
import 'package:flutter_note/note/manager.dart';
import 'package:flutter_note/note/page_home.dart';
import 'package:flutter_note/note/utils.dart';
import 'package:flutter_note/note/widgets.dart';

class ModifyPasswordPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ModifyPasswordPageState();
  }
}

class _ModifyPasswordPageState extends BaseState<ModifyPasswordPage> {
  TextEditingController _newPasswordController; //账号的控制器
  TextEditingController _passController; //密码的控制器
  String username = '';

  @override
  void initState() {
    super.initState();
    _initData();
  }

  _initData() async {
    _newPasswordController = TextEditingController();
    _passController = TextEditingController();
    username = await SPUtils.getString(Const.SP_USERNAME);
    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldkey,
      backgroundColor: color.bg_page,
      appBar: AppBar(
        title: Text('修改密码'),
        elevation: dim.x2,
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              _modifyPassword();
            },
            child: Container(
                color: Colors.transparent,
                width: dim.x60,
                child: Icon(Icons.check)),
          )
        ],
      ),
      body: ListView(
        controller: ScrollController(keepScrollOffset: false),
        children: <Widget>[
          Space(Const.SPACE_TOP),
          lineHorizontal(dim.x1),
          _usernameTextField(),
          lineHorizontal(dim.x1),
          Space(Const.SPACE_TOP),
          lineHorizontal(dim.x1),
          _passwordTextField(),
          lineHorizontal(dim.x1),
          Space(Const.SPACE_TOP),
          lineHorizontal(dim.x1),
          _newPasswordTextField(),
          lineHorizontal(dim.x1),
        ],
      ),
    );
  }

  Widget _usernameTextField() {
    return Container(
      height: dim.x45,
      color: color.white,
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(left: dim.x15),
      child: Text(
        username,
        style: TextStyle(fontSize: dim.t16, color: color.txt3),
      ),
    );
  }

  Widget _passwordTextField() {
    return Container(
      padding: EdgeInsets.only(left: dim.x15, right: dim.x15),
      color: color.white,
      child: TextField(
        maxLines: 1,
        controller: _passController,
        keyboardType: TextInputType.text,
        style: TextStyle(fontSize: dim.t16, color: color.txt3),
        decoration: InputDecoration(
            border: InputBorder.none,
            hintText: '请输入原密码',
            hintStyle: TextStyle(fontSize: dim.t16, color: color.txt9)),
      ),
    );
  }

  Widget _newPasswordTextField() {
    return Container(
      padding: EdgeInsets.only(left: dim.x15, right: dim.x15),
      color: color.white,
      child: TextField(
        maxLines: 1,
        controller: _newPasswordController,
        keyboardType: TextInputType.text,
        style: TextStyle(fontSize: dim.t16, color: color.txt3),
        decoration: InputDecoration(
            border: InputBorder.none,
            hintText: '请输入新密码',
            hintStyle: TextStyle(fontSize: dim.t16, color: color.txt9)),
      ),
    );
  }

  //修改密码请求
  _modifyPassword() async {
    if (_passController.text.isEmpty) {
      showMessage("原密码是空的");
      return;
    }
    if (_newPasswordController.text.isEmpty) {
      showMessage("新密码是空的");
      return;
    }
    show();
    var reply = await UserManager.getInstance().passwordModify(_passController.text, _newPasswordController.text);
    dismiss();
    await Future.delayed(Duration(milliseconds: 50), _modifyResult(reply));
  }

  _modifyResult(Reply reply) {
    if (reply.code == Const.SUCCESS) {
      Navigator.of(context).pop(reply); //修改成功,返回到上一页
    } else {
      showMessage(reply?.msg);
    }
  }
}
